﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_ThongTinCaNhan : System.Web.UI.Page
{
    SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    _dbDataContext _db = new _dbDataContext();
    public string tenhinh = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnclose.Attributes.Add("onClick", "javascript:history.back(); return false;");//them su kien vao  button de quay ve trang truoc
            CatalogAccess.BindItemToDropDown(drldantoc, 1);
            drldantoc.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drltongiao, 2);
            drltongiao.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drlHonnhan, 3);
            drlHonnhan.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drlKhuVuc, 1015);
            BindItemToTrinhDoVanHoa();
            CatalogAccess.BindItemToDropDown(drlNgoaingu, 4);
            CatalogAccess.BindItemToDropDown(drlTinhoc, 5);
            CatalogAccess.BindItemToDropDown(drlChucVuDoan, 9);
            CatalogAccess.BindItemToDropDown(drlTrinhdochuyenmon, 1016);
            BindItemToSucKhoe();
            BindDataToControl();
        }
    }
    private void BindDataToControl()
    {
        string id= _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ","+"),"gold",128);
        _dbDataContext _db = new _dbDataContext();
        var val = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == id);
        txtMaDonVien.Text = val.MaDonVien.Trim();
        txtHoTen.Text = val.HoTen.Trim();
        if ((bool)val.Phai == true)
            nam.Checked = true;
        else
            nu.Checked = true;
        txtngaysinh.Text = string.Format("{0:dd/MM/yyyy}",val.NgaySinh);
        txtnoisinh.Text = (val.NoiSinh!=null?val.NoiSinh.Trim():"");
        try
        {
            txtCMND.Text = val.CMND.Trim();
        }
        catch { }
        if (val.ImgDoanVien != null)
        {
            tenhinh = val.ImgDoanVien.Trim();
        }
        txtngaycapcmnd.Text = string.Format("{0:dd/MM/yyyy}", val.NgayCapCMND);
        try
        {
            txtNoiCap.Text = val.NoiCap.Trim();
        }
        catch { }
        try
        {
            txtNguyenquan.Text = val.NguyenQuan.Trim();
        }
        catch { }
        try
        {
            txtThuongTru.Text = val.HoKhauThuongTru.Trim();
        }
        catch { }
        try
        {
            txtHienNay.Text = val.ChoOHienNay.Trim();
        }
        catch { }
        try
        {
            txtDienthoai.Text = val.DienThoai.Trim();
        }
        catch { }
        try
        {
            txtEmail.Text = val.Email.Trim();
        }
        catch { }
        drldantoc.SelectedValue = val.DanToc.ToString();
        drltongiao.SelectedValue = val.TonGiao.ToString();
        drlHonnhan.SelectedValue = val.HonNhan.ToString();
        try
        {
            txtXuatthan.Text = val.XuatThan.Trim();
        }
        catch { }
        try
        {
            txtNghenghiep.Text = val.NgheNghiep.Trim();
        }
        catch { }
        try
        {
            txtMota.Text = val.MoTaCongViec.Trim();
        }
        catch { }
        drlKhuVuc.SelectedValue = val.DiaBanDanCuID.ToString();
        drlTrinhdovanhoa.SelectedValue = val.TrinhDoVanHoa;
        drlTrinhdochuyenmon.SelectedValue = val.TrinhDoChuyenMonID.ToString();
        chk_dangvien.Checked = (bool)val.DangVien;
        txtbang1.Text = val.BangCap.Split('@')[0];
        txtbang2.Text = val.BangCap.Split('@')[1];
        drlNgoaingu.SelectedValue = val.NgoaiNgu.ToString();
        drlTinhoc.SelectedValue = val.TinHoc.ToString();
        drlSuckhoe.SelectedValue = val.SucKhoe.Trim();
        txtngayvaodoan.Text = string.Format("{0:dd/MM/yyyy}", val.NgayVaoDoan);
        if (bool.Parse(val.PhatTrienMoi.ToString()) == true)
        {
            rdbMoi.Checked = false;
            rdbchuyentoi.Checked = true;
            txtngaychuyenden.Visible = true;
            txtDonViCu.Visible = true;
            txtngaychuyenden.Text = string.Format("{0:dd/MM/yyyy}", val.NgayChuyenDen);
            txtDonViCu.Text = val.DonViCu;
        }
        else
        {
            rdbMoi.Checked = true;
            rdbchuyentoi.Checked = false;
            txtngaychuyenden.Visible = false;
            txtDonViCu.Visible = false;
        }
        txtchucvunoicongtac.Text = val.ChucVuNoiCongTac;
        drlChucVuDoan.SelectedValue = val.ChucVuDoanID.ToString();
    }
        //Add item to drlTrinhDo
    private void BindItemToTrinhDoVanHoa()
    {
        drlTrinhdovanhoa.Items.Clear();
        ListItem item = new ListItem("---Chọn trình độ---", "-1");
        drlTrinhdovanhoa.Items.Add(item);
        for (int i = 1; i <= 12; i++)
        {
            item = new ListItem(i.ToString(), i.ToString());
            drlTrinhdovanhoa.Items.Add(item);
        }
        item = new ListItem("Chưa đi học", "13");
        drlTrinhdovanhoa.Items.Add(item);
    }
    //Add item to drlSuckhoe
    private void BindItemToSucKhoe()
    {
        drlSuckhoe.Items.Clear();
        ListItem item;
        item = new ListItem("Tốt", "1");
        drlSuckhoe.Items.Add(item);
        item = new ListItem("Bình thường", "2");
        drlSuckhoe.Items.Add(item);
        item = new ListItem("Không tốt", "3");
        drlSuckhoe.Items.Add(item);
    }
    protected void rdbMoi_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbMoi.Checked == true)
        {
            txtngaychuyenden.Visible = false;
            txtDonViCu.Visible = false;
            lbldonvicu.Visible = false;
            lblngaychuyen.Visible = false;
        }
    }
    protected void rdbchuyentoi_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbchuyentoi.Checked == true)
        {
            txtngaychuyenden.Visible = true;
            txtDonViCu.Visible = true;
            lbldonvicu.Visible = true;
            lblngaychuyen.Visible = true;
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string filehinh = "";
        if (imgdoanvien.HasFile)
        {
            filehinh = "img" + txtMaDonVien.Text.Trim() + Path.GetExtension(imgdoanvien.FileName);
        }
        bool success = CatalogAccess.UpdateDoanVien(txtMaDonVien.Text, filehinh, txtHoTen.Text, (nam.Checked == true ? true : false), txtngaysinh.Text,
            txtnoisinh.Text, txtCMND.Text, txtngaycapcmnd.Text, txtNoiCap.Text, txtNguyenquan.Text, txtThuongTru.Text, txtHienNay.Text, txtDienthoai.Text,
            txtEmail.Text, int.Parse(drldantoc.SelectedValue.ToString()), int.Parse(drltongiao.SelectedValue.ToString()), int.Parse(drlHonnhan.SelectedValue.ToString()),
            txtXuatthan.Text, txtNghenghiep.Text, txtMota.Text, drlTrinhdovanhoa.SelectedItem.Text, int.Parse(drlTrinhdochuyenmon.SelectedValue.ToString()), txtbang1.Text + "@" + txtbang2.Text,
            int.Parse(drlNgoaingu.SelectedValue.ToString()), int.Parse(drlTinhoc.SelectedValue.ToString()), drlSuckhoe.SelectedItem.Text, txtngayvaodoan.Text, (rdbchuyentoi.Checked == true ? true : false),
            txtngaychuyenden.Text, txtDonViCu.Text, MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name),  int.Parse(drlKhuVuc.SelectedValue.ToString()), chk_dangvien.Checked);
        if (success)
        {
            //luu hinh len server
            if (filehinh != "")
            {
                try
                {
                    imgdoanvien.SaveAs(Server.MapPath(PageURL.local + "imgdoanvien/" + filehinh));
                }
                catch { }
            }
            lblmessage.Text = "Lưu thành công.";
            lblmessage.Visible = true;
        }
        else
        {
            lblmessage.Text = "Không lưu được.";
            lblmessage.Visible = false;
        }
    }
}